var util = require("../../utils/util.js");
const app = getApp();
Page({
  data: {
    navH: app.data.navHeight,
    sex: ["男", "女"],
    index: 0,
    endtime: "0000-00-00",
    codebtn: true,
    codetime: 60,
    jb_sex: "",
    date: "",
    company_ads: "",
    company_name: "",
    duty: "",
    truename: "",
    member_name: "",
    member_mobile: "",
    member_email: "",
    avatar: "",
    selection: []
  },
  /* 修改昵称 */
  getNickname(e) {
    const member_name =
      e.detail.value.length > 0
        ? e.detail.value
        : this.data.usermsg.member_name;
    this.setStorge("member_name", member_name);
    this.setData({
      member_name: member_name
    });
    this.postrequest();
  },
  /* 修改真实姓名 */

  getTrueName(e) {
    const truename =
      e.detail.value.length > 0 ? e.detail.value : this.data.usermsg.truename;
    this.setStorge("truename", truename);
    this.setData({
      truename: truename
    });
    this.postrequest();
  },
  // 修改手机号
  getPhone(e) {
    const member_mobile =
      e.detail.value.length > 0
        ? e.detail.value
        : this.data.usermsg.member_mobile;
    let isMobile = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/.test(
      member_mobile
    );
    if (!isMobile) {
      wx.showToast({
        title: "手机号不合法",
        icon: "none",
        duration: 1000
      });
      return;
    }
    this.setStorge("member_mobile", member_mobile);
    this.setData({
      member_mobile: member_mobile
    });
    this.postrequest();
  },
  // 修改邮箱
  getEmail(e) {
    const member_email = e.detail.value.length > 0 ? e.detail.value : this.data.usermsg.member_email;
    let isEmail = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(member_email)
    if(!isEmail){
      wx.showToast({
        title: "邮箱不合法",
        icon: "none",
        duration: 1000
      });
      return
    }
    this.setStorge("member_email", member_email);
    this.setData({
      member_email: member_email
    });
    this.postrequest();
  },
  /* 修改地址 */

  getCompanyAds(e) {
    const company_ads =
      e.detail.value.length > 0
        ? e.detail.value
        : this.data.usermsg.company_ads;
    this.setStorge("company_ads", company_ads);
    this.setData({
      company_ads: company_ads
    });
    this.postrequest();
  },
  /* 修改公司名称 */

  getCompanyName(e) {
    const company_name =
      e.detail.value.length > 0
        ? e.detail.value
        : this.data.usermsg.company_name;
    this.setStorge("company_name", company_name);
    this.setData({
      company_name: company_name
    });
    this.postrequest();
  },
  /* 修改职位 */

  getjob(e) {
    const duty =
      e.detail.value.length > 0 ? e.detail.value : this.data.usermsg.duty;
    this.setStorge("duty", duty);
    this.setData({
      duty: duty
    });
    this.postrequest();
  },
  //封装の修改信息
  setStorge(name, data) {
    let info = wx.getStorageSync("vs-info");
    info[name] = data;
    wx.setStorageSync("vs-info", info);
    this.setData({
      usermsg: info
    });
  },

  //发送验证码
  sendCode() {
    wx.$.fetch("api/sendSmsCode", {
      method: "post",
      data: {
        phone: this.data.member_mobile,
        log_type: 1,
        api_token: wx.getStorageSync("vs-token")
      }
    }).then(res => {
      if (res.data.state == 200) {
        wx.showToast({
          title: res.data.msg
        });
      }
    });
    this.setData({
      getView: !this.data.geting
    });
    var timer = setInterval(() => {
      this.setData({
        codetime: (this.data.codetime -= 1)
      });
      if (this.data.codetime <= 0) {
        clearInterval(timer);
        this.setData({
          codetime: 60,
          getView: false
        });
      }
    }, 30);
  },
  // 获取验证码
  getCode(e) {
    const code = e.detail.value;
    this.setData({
      code: code
    });
  },

  //保存按钮验证
  sumTel() {
    if (this.data.member_mobile && this.data.code) {
      wx.$.fetch(`api/validateCode`, {
        method: "post",
        data: {
          phone: this.data.member_mobile,
          code: this.data.code,
          api_token: wx.getStorageSync("vs-token")
        }
      }).then(res => {
        if (res.data.state == 200) {
          wx.showToast({
            title: res.data.msg
          });
          this.setData({
            showView: !this.data.showView,
            appiphone: this.data.member_mobile,
            code: ""
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      });
    } else {
      wx.showToast({
        title: "请输入完整",
        icon: "none"
      });
    }
  },

  //  点击性别确定事件
  bindPickerChange: function(e) {
    var index = e.detail.value;
    var indexs = index == 0 ? 1 : 2;
    this.setData({
      sexSelect: this.data.sex[index],
      jb_sex: indexs
    });
    this.postrequest();
  },
  //  点击生日确定事件
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    });
    const birthday = e.detail.value;
    this.setStorge("birthday", birthday);
    this.postrequest();
  },

  subscribeOn: function() {
    var that = this;
    that.setData({
      showView: !that.data.showView
    });
  },
  /* 跳转裁剪页面 */
  getTailoring() {
    this.data.cuttype = null;
    this.data.licenceCard = null;
    wx.navigateTo({
      url: "/pages/cutFace/index?cuttype=3&touxiang=true"
    });
  },
  /* 更换头像 */
  upLoadImg(tempFilePath) {
    var _this = this;
    wx.uploadFile({
      url: wx.$.host + "api/uploadPicQcloud",
      filePath: tempFilePath,
      name: "files",
      success: function(res) {
        if (res.data) {
          let data = JSON.parse(res.data);
          _this.setStorge("avatar", data.path);
          _this.setData({
            avatar: data.path
          });
          _this.postrequest();
        }
      }
    });
  },

  postrequest() {
    wx.$.fetch("api/my/editInfo", {
      method: "post",
      data: {
        sex: this.data.jb_sex,
        birthday: this.data.date,
        company_ads: this.data.company_ads,
        company_name: this.data.company_name,
        duty: this.data.duty,
        truename: this.data.truename,
        member_name: this.data.member_name,
        mobile: this.data.member_mobile,
        email: this.data.member_email,
        avatar: this.data.avatar
      }
    }).then(res => {
      // if (res.data.medal.medal_id !== 0 && res.data.medal.medal_id !== "0") {
      //   this.btnExhibitionMedal(res.data.medal.medal_id)
      // }
    });
  },
  onLoad() {
    if (app.data.jsonPage) {
      var nvabarData = {
        title: app.data.jsonPage.message.title,
        bgclase: false,
        showCapsule: true,
        textclase: false,
        texttype: false
      };
      let message = app.data.jsonPage.message
      this.setData({
        nvabarData: nvabarData,
        namelist: message.namelist
      });
    } else {
      var left = this;
      app.getjsonPage = res => {
        var nvabarData = {
          title: res.message.title,
          bgclase: false,
          showCapsule: true,
          textclase: false,
          texttype: false
        };
        left.setData({
          nvabarData: nvabarData,
          namelist: res.message.namelist
        });
      };
    }
    // let message = app.data.jsonPage.message;
    // var nvabarData = {
    //   title: message.title,
    //   showCapsule: true
    // }
    // this.setData({
    //   nvabarData: nvabarData,
    //   namelist: message.namelist,
    // })
  },
  onShow() {
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    const info = wx.getStorageSync("vs-info");
    this.setData({
      time: time,
      usermsg: info
    });
    if (this.data.cuttype == 1) {
      this.upLoadImg(this.data.IDCard);
    }
    if (this.data.cuttype == 3) {
      this.upLoadImg(this.data.licenceCard);
    }
    if (this.data.cuttype == 4) {
      this.upLoadImg(this.data.certificationCard);
    }
    this.Obtain();
  },
  /* 修改标签 */
  AddLabel() {
    wx.navigateTo({
      url: "/pages/messageLabel/messageLabel"
    });
  },
  /* 获取标签数组 */
  Obtain() {
    wx.$.fetch("api/my/memberTagList", {
      hideLoading: true,
      method: "get"
    }).then(res => {
      var selection = [];
      for (var i = 0; i < res.data.data.length; i++) {
        if (res.data.data[i].checked > 0) {
          selection.push(res.data.data[i].name);
        }
      }
      this.setData({
        selection: selection
      });
    });
  },
  /* 选中 */
  btnSelection(e) {
    var name = e.currentTarget.dataset.name;
    var of = this.data.selection.indexOf(name);
    if (of !== -1) {
      this.data.selection.splice(of, 1);
    }
    this.btnSubmission();
  },
  /* 提交 */
  btnSubmission() {
    wx.$.fetch("api/my/memberTagUpdate", {
      hideLoading: true,
      method: "post",
      data: {
        tags: this.data.selection
      }
    }).then(res => {
      if (res.data.state == 0) {
        this.setData({
          selection: this.data.selection
        });
        wx.showToast({
          title: res.data.msg,
          icon: "none",
          mask: true,
          duration: 2000
        });
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none",
          mask: true,
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {}
});
